import axios from 'axios'
import { showNotify } from 'vant'
import { getToken, removeToken } from '@/utils/token.js'
import router from '@/router'

const request = axios.create({
    baseURL: 'http://124.220.178.79:8199',
    timeout: 15000,
})

//请求拦截
request.interceptors.request.use((config) => {
    config.headers.Authorization = `Bearer ${getToken()}`
    return config
}, error => Promise.reject(error))

// 添加响应拦截器
request.interceptors.response.use(
    res => {
        if (res.data.code === 1) {
            return res.data
        } else {
            if (res.data.code === 0) {
                showNotify({ type: 'danger', message: res.data.message || '请求异常' });
                if (res.data.message === '请登录') {
                    removeToken()
                    router.replace('/login')
                }
            }
            if (res.data.code === 3) {
                showNotify({ type: 'danger', message: "登录信息有误，请检查重试" });
            }
            return Promise.reject("调用失败")
        }

    },
    error => Promise.reject(error)
);



export default request;

